package com.xuetang9.t9.hotel.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.xuetang9.t9.hotel.beans.EStaffinfo;
import com.xuetang9.t9.hotel.services.IStaffinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName StaffintoController
 * @Description TODO
 * @Author qin
 * @Date 2020/2/25 11:05
 * @Version 1.0
 **/
@Controller
@RequestMapping("staff")
public class StaffintoController {
    @Autowired
    private IStaffinfoService staffinfoService;

    @PostMapping("list")
    @ResponseBody
    public List<EStaffinfo> list(){

        return staffinfoService.queryStaffList();
    }

    @PostMapping("insert")
    @ResponseBody
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public List<EStaffinfo> insert(@RequestBody EStaffinfo eStaffinfo) throws ParseException {
        int id = staffinfoService.queryCount()+1;
        eStaffinfo.setId(id);
        staffinfoService.insertStaff(eStaffinfo);
        return staffinfoService.queryStaffList();
    }

    @PostMapping("update")
    @ResponseBody
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public List<EStaffinfo> update(@RequestBody EStaffinfo staffinfo) throws ParseException {
        staffinfoService.updateStaff(staffinfo);
        return staffinfoService.queryStaffList();
    }

    @PostMapping("delete")
    @ResponseBody
    public List<EStaffinfo> delete(@RequestParam("id")String id){
        System.out.println(id);
        staffinfoService.deleteStaff(Integer.valueOf(id));
        return staffinfoService.queryStaffList();
    }

    @PostMapping("loginName")
    @ResponseBody
    private EStaffinfo selectName(@RequestParam("name") String name){
        return staffinfoService.queryLogin(name);
    }

    @PostMapping("name")
    @ResponseBody
    private EStaffinfo updateName(@RequestParam("name") String name,@RequestParam("id") Integer id){
        System.out.println(name);
        System.out.println(id);
        boolean result = staffinfoService.updateName(name,id);
        if (result){
            return staffinfoService.queryById(id);
        }else {
            return null;
        }
    }
    @PostMapping("pass")
    @ResponseBody
    private EStaffinfo updatePass(@RequestParam("pass") String pass,@RequestParam("id") Integer id){
        System.out.println(pass);
        System.out.println(id);
        boolean result = staffinfoService.updatePass(pass,id);
        if (result){
            return staffinfoService.queryById(id);
        }else {
            return null;
        }
    }
    @PostMapping("phone")
    @ResponseBody
    private EStaffinfo updatePhone(@RequestParam("phone") String phone,@RequestParam("id") Integer id){
        System.out.println(phone);
        System.out.println(id);
        boolean result = staffinfoService.updatePhone(phone,id);
        if (result){
            return staffinfoService.queryById(id);
        }else {
            return null;
        }
    }

}
